package de.betagames.dreamworld.charactereditor.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import de.betagames.dreamworld.charactereditor.model.Character;
import de.betagames.dreamworld.main.Main;

public class CharacterEditor extends JFrame {

	private Character c;
	private JLabel hp, mana, dodge, dmg, _int, _str, _dex, _con, portrait,
			hpValue, manaValue, dodgeValue, dmgValue;

	private JTextField strValue, intValue, dexValue, conValue, name;

	private JButton accept, dismiss, randomize, strPlus, strMinus, intPlus,
			intMinus, conPlus, conMinus, dexPlus, dexMinus;

	public CharacterEditor() {

		setSize(480, 240);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setLocationRelativeTo(null);
		setVisible(true);
		setEnabled(false);

		addWindowListener(new WindowListener() {

			@Override
			public void windowOpened(WindowEvent arg0) {

			}

			@Override
			public void windowIconified(WindowEvent arg0) {

			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {

			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {

			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				Object[] options = { "Yes, please", "No, thanks", "Abort" };
				int n = JOptionPane
						.showOptionDialog(getParent(),
								"Do you want to go back to the menu?",
								"Close?", JOptionPane.YES_NO_CANCEL_OPTION,
								JOptionPane.QUESTION_MESSAGE, null, options,
								options[2]);

				if (n == JOptionPane.NO_OPTION) {
					System.exit(0);
				} else if (n == JOptionPane.YES_OPTION) {
					new Main();
					dispose();
				}
			}

			@Override
			public void windowClosed(WindowEvent arg0) {

			}

			@Override
			public void windowActivated(WindowEvent arg0) {

			}
		});

		init();

		new CharacterChooser(this);
	}

	public void init() {
		setLayout(new BorderLayout(5, 5));
		initSouth();
		initEast();
		initCenter();
	}

	private void initSouth() {

		JPanel south = new JPanel();

		south.setLayout(new GridLayout(1, 3, 5, 5));

		accept = new JButton("Accept");
		dismiss = new JButton("Dismiss");
		randomize = new JButton("Randomize");

		accept.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				saveCharacter();
			}
		});

		south.add(accept);
		south.add(dismiss);
		south.add(randomize);

		add(south, BorderLayout.SOUTH);

	}

	public void initEast() {
		JPanel east = new JPanel();
		east.setLayout(null);
		portrait = new JLabel();
		portrait.setBounds(5, 5, 100, 100);
		portrait.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
		east.add(portrait);
		east.setPreferredSize(new Dimension(110, 110));
		add(east, BorderLayout.EAST);
	}

	public void initCenter() {
		JPanel center = new JPanel();

		_str = new JLabel("Strength");
		_int = new JLabel("Intelligence");
		_con = new JLabel("Constitution");
		_dex = new JLabel("Dexterity");

		strPlus = new JButton("+");
		strValue = new JTextField("5");
		strMinus = new JButton("-");

		conPlus = new JButton("+");
		conValue = new JTextField("5");
		conMinus = new JButton("-");

		intPlus = new JButton("+");
		intValue = new JTextField("5");
		intMinus = new JButton("-");

		dexPlus = new JButton("+");
		dexValue = new JTextField("5");
		dexMinus = new JButton("-");

		center.setLayout(new GridLayout(4, 2, 5, 5));
		center.add(_str);
		JPanel tmp = new JPanel();
		tmp.setLayout(new GridLayout(1, 3, 5, 5));
		tmp.add(strPlus);
		tmp.add(strValue);
		tmp.add(strMinus);
		center.add(tmp);
		center.add(_con);
		JPanel tmp2 = new JPanel();
		tmp2.setLayout(new GridLayout(1, 3, 5, 5));
		tmp2.add(conPlus);
		tmp2.add(conValue);
		tmp2.add(conMinus);
		center.add(tmp2);
		center.add(_dex);
		JPanel tmp3 = new JPanel();
		tmp3.setLayout(new GridLayout(1, 3, 5, 5));
		tmp3.add(dexPlus);
		tmp3.add(dexValue);
		tmp3.add(dexMinus);
		center.add(tmp3);
		center.add(_int);
		JPanel tmp4 = new JPanel();
		tmp4.setLayout(new GridLayout(1, 3, 5, 5));
		tmp4.add(intPlus);
		tmp4.add(intValue);
		tmp4.add(intMinus);
		center.add(tmp4);

		add(center, BorderLayout.CENTER);
	}

	public void setChar(String character) {
		setEnabled(true);
		c = new Character();
		loadCharacter(character);
	}

	private void loadCharacter(String character) {

		try {
			Properties charProps = new Properties();
			charProps.load(new FileInputStream(new File(System
					.getProperty("user.dir")
					+ "/Characters/"
					+ character
					+ ".char")));
			c.setCon(Integer.valueOf(charProps.getProperty("con")));
			conValue.setText("" + c.getCon());
			c.setDex(Integer.valueOf(charProps.getProperty("dex")));
			dexValue.setText("" + c.getDex());
			c.setStr(Integer.valueOf(charProps.getProperty("str")));
			strValue.setText("" + c.getStr());
			c.setInt(Integer.valueOf(charProps.getProperty("int")));
			intValue.setText("" + c.getInt());
			c.setName(charProps.getProperty("name"));
			// name.setText("" + c.getCon());
			c.setPortrait(charProps.getProperty("portrait"));
		} catch (Exception e) {
			JOptionPane
					.showMessageDialog(
							this,
							"<html>Somehow the dreaming integrity of your Dream-Ego has been disturbed.<br>"
									+ "You should review it.</html>", "ERROR",
							JOptionPane.ERROR_MESSAGE, null);
			setEnabled(false);
			new CharacterChooser(this);
		}
	}

	public void saveCharacter() {
		c.save();
	}

}
